<?php

use Biz\model\Album\AlbumListModel;
use Biz\service\Album\FormatAlbumService;
use Doraemon\model\Favorite as DbFavorite;
use Doraemon\service\Favorite as FavoriteService;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\service\User;
use Doraemon\tools\BlockType;
use Doraemon\tools\Hhzurl;

class FavListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;
    const PAGE_SIZE = 20;

    /**
     * @throws \Exception
     */
    public function main()
    {
        $currUid = $this->uid;
        $uid = (int)trim($this->getRequest()->getPost('uid', 0));
        $startId = $this->getRequest()->getPost('start_id', 0);

        if ($uid == 0) { //如果没有uid参数 则uid默认为当前登录用户uid
            $uid = $currUid;
        } else {
            $userInfo = User::getUser($uid);
            if (!$userInfo) {
                throw new Exception("用户不存在", self::CODE_ERROR_VIEW_TOSTA);
            }
        }
        $count = FavoriteService::getCountByType($uid, DbFavorite::TYPE_WIKI_ALBUM);
        if ($count) {
            $resultList = FavoriteService::getFavoriteListByType($uid, $startId, DbFavorite::TYPE_WIKI_ALBUM,
                self::PAGE_SIZE);
            $albumIds = [];
            foreach ($resultList['rows'] as $val) {
                $albumIds[] = $val['obj_id'];
            }
            if($uid == $currUid){//查看自己的
                $where['status[!]'] = AlbumListModel::STATUS_DELETE;
            }else{ //查看别人的
                $where['status'] = AlbumListModel::STATS_PUBLIC_SHOW_MAP;
            }
            $where['album_id'] = $albumIds;
            $where['ORDER'] = ['album_id' => $albumIds];
            $albumInfos = AlbumListModel::selectByWhere($where);
            if($albumInfos) {
                $formatInfo = FormatAlbumService::_formatAlbumList($albumInfos , FormatAlbumService::SELF_FAV_LIST);
                foreach ($formatInfo as $key => $val) {
                    //如果该清单不是当前用户自己的 并且状态是11 则跳过
                    if($val['album_info']['uid'] != $currUid && AlbumListModel::STATUS_PRIVATE == $val['album_info']['status']){
                        continue;
                    }
                    $val['statSign'] = basicStatSign::genStatSign(basicStatSign::MY_ALBUM_PAGE_ALBUM_LIST, [
                        basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI_ALBUM,
                        basicStatSign::EXTEND_KEY_OBJ_ID => $val['album_info']['album_id'],
                        basicStatSign::EXTEND_KEY_INDEX => $key,
                    ], ['tab_name' => 'fav']);
                    $data['list'][] = $val;
                }
            }
            $data['total'] = $count;
            if ($data['list']) {
                $data['is_over'] = count($resultList['rows']) > 0 ? 0 : 1;
                $data['last_id'] = $resultList['rows'][count($resultList['rows']) - 1]['id'];
            } else {
                $data = array(
                    'list' => array(),
                    'is_over' => 1,
                );
            }
        } else {
            $data = array(
                'list' => array(),
                'is_over' => 1,
            );
        }
        $this->setSuccess($data, self::API_CODE_SUCCES);


    }
}